//@Author:code-cats 

#include "control.h"
#include "main.h"
#include "display.h"
#include "i2c_hal.h"
#include "tim.h"
#include "stdio.h"

uint16_t button_count[4];
uint8_t button_flag[4];
void button_deal(void)	//10ms cycle
{
	if(HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin)==0)
	{
		button_count[0]++;
		if(button_count[0]==2)
		{
			button_flag[0]=1;
		}
	}
	else
	{
		button_count[0]=0;
	}
	
	if(HAL_GPIO_ReadPin(B2_GPIO_Port,B2_Pin)==0)
	{
		button_count[1]++;
		if(button_count[1]==2)
		{
			button_flag[1]=1;
		}
	}
	else
	{
		button_count[1]=0;
	}
	
	if(HAL_GPIO_ReadPin(B3_GPIO_Port,B3_Pin)==0)
	{
		button_count[2]++;
		if(button_count[2]==2)
		{
			button_flag[2]=1;
		}
	}
	else
	{
		button_count[2]=0;
	}
	
	if(HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin)==0)
	{
		button_count[3]++;
		if(button_count[3]==2)
		{
			button_flag[3]=1;
		}
	}
	else
	{
		button_count[3]=0;
	}
}

//It's better to use a structure here
uint8_t shopx=0;
uint8_t shopy=0;
float pricex=1.0;
float pricey=1.0;
uint8_t repx=10;
uint8_t repy=10;

uint8_t repxy_write_flag=0;

uint8_t led1=1;
uint8_t led1_5s_flag=0;
uint32_t led1_5s_starttime=0;
uint8_t led2=1;

uint8_t uart_sendbuy_flag=0;	//send shopping information when press buy button
extern char uartsendbuy[20];

uint32_t time_1ms_count=0;
uint8_t sys_state=0;	//0:SHOP 1:PRICE 2:REP
void time_1ms_callback(void)
{
	time_1ms_count++;
	
	if(time_1ms_count%10==0)
	{
		button_deal();
	}
	
	
	if(led1_5s_flag==1)
	{
		if(time_1ms_count-led1_5s_starttime<5000)
		{
			led1=0;	//light
			__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,30);
		}
		else
		{
			led1_5s_flag=0;
		}
	}
	else
	{
		led1=1;	//not light
		__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,5);
	}
	
	if(repx==0&&repy==0)
	{
		if(time_1ms_count%100==0)
		{
			led2=!led2;
		}
	}
	else
	{
		led2=1;
	}
	
	switch(sys_state)
	{
		case 0:	//SHOP
		{
			if(button_flag[1]==1)	//B2 x
			{
				shopx++;
				if(shopx>repx) shopx=0;
				button_flag[1]=0;
			}
			if(button_flag[2]==1)	//B3 y
			{
				shopy++;
				if(shopy>repy) shopy=0;
				button_flag[2]=0;
			}
			if(button_flag[3]==1)	//B4 buy
			{
				repx-=shopx;
				repy-=shopy;
				uart_sendbuy_flag=1;
				sprintf(uartsendbuy,"X:%d,Y:%d,Z:%.1f",shopx,shopy,(float)(shopx*pricex+shopy*pricey));	//Write data here and send it in main
				shopx=0;
				shopy=0;
				repxy_write_flag=1;	//write eeprom when both repx and repy change
				led1_5s_starttime=time_1ms_count;	//record the start time of 5 seconds
				led1_5s_flag=1;
				
				button_flag[3]=0;
			}
			
			if(button_flag[0]==1)	//B1
			{
				button_flag[0]=0;
				sys_state=1;
			}
			break;
		}
		case 1:	//PRICE
		{
			if(button_flag[1]==1)	//B2 x
			{
				pricex+=0.1f;
				if(pricex>2.001f) pricex=1.0f;
				uint8_t priceint=(uint8_t)(pricex*10);
				E2PROM_Write(2,priceint);
				button_flag[1]=0;
			}
			if(button_flag[2]==1)	//B3 y
			{
				pricey+=0.1f;
				if(pricey>2.001f) pricey=1.0f;
				uint8_t priceint=(uint8_t)(pricey*10);
				E2PROM_Write(3,priceint);
				button_flag[2]=0;
			}
			
			if(button_flag[0]==1)	//B1
			{
				button_flag[0]=0;
				sys_state=2;
			}
			
			if(button_flag[3]==1)	//B4 buy
			{
				button_flag[3]=0;
			}
			break;
		}
		case 2:
		{
			if(button_flag[1]==1)	//B2 x
			{
				repx++;
				E2PROM_Write(0,repx);
				button_flag[1]=0;
			}
			if(button_flag[2]==1)	//B3 y
			{
				repy++;
				E2PROM_Write(1,repy);
				button_flag[2]=0;
			}
			
			if(button_flag[0]==1)	//B1
			{
				button_flag[0]=0;
				sys_state=0;
			}
			
			if(button_flag[3]==1)	//B4 buy
			{
				button_flag[3]=0;
			}
			break;
		}
	}
}

//This feature is not required during matches
void recover_from_e2prom(void)
{
	uint8_t val=0;
	repx=E2PROM_Read(0);
	HAL_Delay(10);
	
	repy=E2PROM_Read(1);
	HAL_Delay(10);
	
	val=E2PROM_Read(2);
	pricex=(float)(val/10.0f);
	if(pricex<1.0f) pricex=1.0f;
	if(pricex>2.001f) pricex=1.0f;
	
	HAL_Delay(10);
	
	val=E2PROM_Read(3);
	pricey=(float)(val/10.0f);
	if(pricey<1.0f) pricey=1.0f;
	if(pricey>2.001f) pricey=1.0f;
	HAL_Delay(10);
}
